package com.my.radio;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import com.my.radio.R;
public class MarkFaceView extends View {
	private Paint mPaint;
	public static float mFrequencyNum;
	private static int FM_X_VALUE = 21;
	private static int FM_Y_VALUE = 1705;
	private static float AM_X_VALUE = (float) 0.4;
	private static  int AM_Y_VALUE = 174;
	private static final int POINTER_WIDTH = 2;
	private static int POINTER_HEIGHT = 180;
	private static final int POINTER_Y = 10;

	private static final int MAX_FM_FREQUENCY = 10800;
	private static final int MIN_FM_FREQUENCY = 8750;
	private static final int MAX_AM_FREQUENCY = 1629;
	private static final int MIN_AM_FREQUENCY = 531;

	private static final int MAX_FM_FREQUENCY_EAST_EU = 7400;
	private static final int MIN_FM_FREQUENCY_EAST_EU = 6500;
	private static final int MAX_FM_FREQUENCY_JPN = 9000;
	private static final int MIN_FM_FREQUENCY_JPN = 7600;
	
	private short mMaxFmFrequency = MAX_FM_FREQUENCY;
	private short mMinFmFrequency = MIN_FM_FREQUENCY;	
	
	private int mFmXValue = FM_X_VALUE;
	private int mFmYValue = FM_Y_VALUE;
	
	private boolean mIsAM;
	private float mRealNum;
	private boolean mIsInTouch;

	private RadioActivity mRadioActivity;
	
	public MarkFaceView(Context context) {
		super(context);
	}

	public MarkFaceView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mPaint = new Paint();
		POINTER_HEIGHT = context.getResources().getInteger(R.integer.point_height);
		FM_X_VALUE = context.getResources().getInteger(R.integer.fm_x);
		FM_Y_VALUE = context.getResources().getInteger(R.integer.fm_y);
		AM_X_VALUE = (float)(context.getResources().getInteger(R.integer.am_x)/100.0f);
		AM_Y_VALUE = context.getResources().getInteger(R.integer.am_y);
		
		FREQ_Y = context.getResources().getInteger(R.integer.freq_y);
		// setBackgroundResource(R.drawable.markface);
	}

	public MarkFaceView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	private static int FREQ_Y = 170;
	@Override
	protected void onDraw(Canvas canvas) {
//		Drawable myImage;		
//
//		myImage = getContext().getResources().getDrawable(R.drawable.markface);
//		myImage.setBounds(0, 0, myImage.getIntrinsicWidth(), myImage
//				.getIntrinsicHeight());
//		myImage.draw(canvas);
		mPaint.setColor(Color.parseColor("#FFFFFF"));
		mPaint.setTextSize(18);
		mPaint.setAntiAlias(true);
		if (mIsAM) {
			if (!mIsInTouch && mFrequencyNum != 0) {
				mRealNum = mFrequencyNum * AM_X_VALUE - AM_Y_VALUE
						+ POINTER_WIDTH;
			}
			canvas.drawText("500", 18, FREQ_Y, mPaint);
			canvas.drawText("750", 143, FREQ_Y, mPaint);
			canvas.drawText("1000", 268, FREQ_Y, mPaint);
			canvas.drawText("1250", 388, FREQ_Y, mPaint);
			canvas.drawText("1500", 515, FREQ_Y, mPaint);
			canvas.drawText("1750", 645, FREQ_Y, mPaint);
		} else {
			if (!mIsInTouch && mFrequencyNum != 0) {
				mRealNum = mFrequencyNum * mFmXValue - mFmYValue
						+ POINTER_WIDTH;
			}
			
			if (mMaxFmFrequency == MAX_FM_FREQUENCY_EAST_EU
					&& mMinFmFrequency == MIN_FM_FREQUENCY_EAST_EU) {				
				canvas.drawText("65", 18, FREQ_Y, mPaint);
				canvas.drawText("67", 118, FREQ_Y, mPaint);
				canvas.drawText("69", 218, FREQ_Y, mPaint);
				canvas.drawText("71", 315, FREQ_Y, mPaint);
				canvas.drawText("73", 415, FREQ_Y, mPaint);
				canvas.drawText("75", 515, FREQ_Y, mPaint);
				
			} else if (mMaxFmFrequency == MAX_FM_FREQUENCY_JPN
					&& mMinFmFrequency == MIN_FM_FREQUENCY_JPN) {
				canvas.drawText("75", 18, FREQ_Y, mPaint);
				canvas.drawText("78", 118, FREQ_Y, mPaint);
				canvas.drawText("81", 218, FREQ_Y, mPaint);
				canvas.drawText("84", 315, FREQ_Y, mPaint);
				canvas.drawText("87", 415, FREQ_Y, mPaint);
				canvas.drawText("90", 515, FREQ_Y, mPaint);
			} else {
				canvas.drawText("85", 18, FREQ_Y, mPaint);
				canvas.drawText("90", 143, FREQ_Y, mPaint);
				canvas.drawText("95", 268, FREQ_Y, mPaint);
				canvas.drawText("100", 388, FREQ_Y, mPaint);
				canvas.drawText("105", 515, FREQ_Y, mPaint);
				canvas.drawText("110", 645, FREQ_Y, mPaint);
			}
			
		}
		
		mPaint.setColor(Color.parseColor("#F02323"));
		canvas.drawRect(mRealNum - POINTER_WIDTH, POINTER_Y, mRealNum, POINTER_HEIGHT,
				mPaint);
		mPaint.setColor(android.R.color.black);
		mPaint.setAlpha(80);
		canvas.drawLine(mRealNum, POINTER_Y, mRealNum, POINTER_HEIGHT, mPaint);
		mPaint.setAlpha(50);
		canvas.drawLine(mRealNum + 1, POINTER_Y, mRealNum + 1, POINTER_HEIGHT, mPaint);
		mPaint.setAlpha(10);
		canvas.drawLine(mRealNum + 2, POINTER_Y, mRealNum + 2, POINTER_HEIGHT, mPaint);
		// super.onDraw(canvas);
	}

	public boolean ismIsAM() {
		return mIsAM;
	}

	public void setmIsAM(boolean mIsAM) {
		this.mIsAM = mIsAM;
	}
	
	public boolean setFmFrequencyRange(short min, short max){
		if ((max == MAX_FM_FREQUENCY && min == MIN_FM_FREQUENCY)
				|| (max == MAX_FM_FREQUENCY_EAST_EU && min == MIN_FM_FREQUENCY_EAST_EU)
				|| (max == MAX_FM_FREQUENCY_JPN && min == MIN_FM_FREQUENCY_JPN)) {
			mMaxFmFrequency = max;
			mMinFmFrequency = min;
			if (mMaxFmFrequency == MAX_FM_FREQUENCY_EAST_EU
					&& mMinFmFrequency == MIN_FM_FREQUENCY_EAST_EU) {				
				mFmXValue = 50;
				mFmYValue = 3224;				
			} else if (mMaxFmFrequency == MAX_FM_FREQUENCY_JPN
					&& mMinFmFrequency == MIN_FM_FREQUENCY_JPN) {
				mFmXValue = 33;
				mFmYValue = 2483;
			} else {
				mFmXValue = FM_X_VALUE;
				mFmYValue = FM_Y_VALUE;
			}
			return true;
		}
		return false;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		final int action = event.getAction();

		switch (action & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			mIsInTouch = true;
			break;
		case MotionEvent.ACTION_MOVE:
			short tmp_frequency;
			if (!mIsAM) {
				tmp_frequency = (short) (((event.getX() + mFmYValue) / mFmXValue) * 100);
				if (tmp_frequency > mMaxFmFrequency) {
					tmp_frequency = mMaxFmFrequency;
				} else if (tmp_frequency < mMinFmFrequency) {
					tmp_frequency = mMinFmFrequency;
				}
				mRealNum = (tmp_frequency * mFmXValue / 100) - mFmYValue
						+ POINTER_WIDTH;
			} else {
				tmp_frequency = (short) ((event.getX() + AM_Y_VALUE) / AM_X_VALUE);
				if (tmp_frequency > MAX_AM_FREQUENCY) {
					tmp_frequency = MAX_AM_FREQUENCY;
				} else if (tmp_frequency < MIN_AM_FREQUENCY) {
					tmp_frequency = MIN_AM_FREQUENCY;
				}
				mRealNum = (tmp_frequency * AM_X_VALUE) - AM_Y_VALUE
						+ POINTER_WIDTH;
			}
			if (mRadioActivity != null){
				mRadioActivity.setFrequencyNumber(tmp_frequency, false);
			}
			postInvalidate();
			break;
		case MotionEvent.ACTION_UP:
			int t_frequency;
			if (!mIsAM){
				tmp_frequency = (short) (((event.getX() + mFmYValue) / mFmXValue) * 100);
				if (tmp_frequency > mMaxFmFrequency) {
					tmp_frequency = mMaxFmFrequency;
				} else if (tmp_frequency < mMinFmFrequency) {
					tmp_frequency = mMinFmFrequency;
				}
			}
			else{
				tmp_frequency = (short) ((event.getX() + AM_Y_VALUE) / AM_X_VALUE);
				if (tmp_frequency > MAX_AM_FREQUENCY) {
					tmp_frequency = MAX_AM_FREQUENCY;
				} else if (tmp_frequency < MIN_AM_FREQUENCY) {
					tmp_frequency = MIN_AM_FREQUENCY;
				}
			}
			mIsInTouch = false;
			if (mRadioActivity != null){
				mRadioActivity.setFrequencyNumber(tmp_frequency, true);
			}
			mFrequencyNum = 0;
			// postInvalidate();
			break;
		}
		return true;
	}
	public void setRaidoActivity(RadioActivity ac){
		mRadioActivity = ac;
	}

}
